#! /bin/sh /usr/share/dpatch/dpatch-run
## 65-sieveshell-enhancements.dpatch by Sven Mueller <debian@incase.de>
## Hacked up to 2.3.13 by Duncan Gibb <duncan.gibb@siriusit.co.uk>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Adds some enhancements to sieveshell and fixes some paths.

@DPATCH@
--- cyrus-imapd-2.4.orig/perl/imap/IMAP/Shell.pm
+++ cyrus-imapd-2.4/perl/imap/IMAP/Shell.pm
@@ -439,7 +439,7 @@ sub run {
 sub shell {
   my ($server, $port, $authz, $auth, $systemrc, $userrc, $dorc, $mech, $pw,
       $tlskey, $notls) =
-    ('', 143, undef, $ENV{USER} || $ENV{LOGNAME}, '/usr/local/etc/cyradmrc.pl',
+    ('', 143, undef, $ENV{USER} || $ENV{LOGNAME}, '/etc/cyradmrc.pl',
      "$ENV{HOME}/.cyradmrc.pl", 1, undef, undef, undef, undef);
   GetOptions('user|u=s' => \$auth,
 	     'authz|z=s' => \$authz,
@@ -472,7 +472,7 @@ sub shell {
     $cyradm->authenticate(-authz => $authz, -user => $auth,
 			  -mechanism => $mech, -password => $pw,
 			  -tlskey => $tlskey, -notls => $notls)
-      or die "cyradm: cannot authenticate to server with $mech as $auth\n";
+      or die "cyradm: cannot authenticate to server" . (defined($mech)?" with $mech":"") . " as $auth\n";
   }
   my $fstk = [*STDIN, *STDOUT, *STDERR];
   if ($dorc && $systemrc ne '' && -f $systemrc) {
--- cyrus-imapd-2.4.orig/perl/sieve/scripts/sieveshell.pl
+++ cyrus-imapd-2.4/perl/sieve/scripts/sieveshell.pl
@@ -61,7 +61,9 @@ my $deletehelp =     "delete <name>    -
 my $username = $ENV{USER};
 my $authname = $ENV{USER};
 my $realm = "";
+my $password;
 my $ex = "";
+my $exfile = "";
 my $help = 0;
 my $man = 0;
 my $ret;
@@ -69,7 +71,9 @@ my $ret;
 GetOptions("a|authname:s" => \$authname,
     "u|username:s" => \$username,
     "r|realm:s" => \$realm,
+    "p|password:s" => \$password,
     "e|exec:s" => \$ex,
+    "f|execfile:s" => \$exfile,
     "help|?" => \$help,
     man => \$man) or pod2usage(2);
 pod2usage(1) if $help;
@@ -84,20 +88,24 @@ my $acapserver = $ARGV[0];
 my $filehandle;
 my $interactive;
 
-if (! $ex eq "") {
-    $filehandle = tempfile();
-
-    if (!$filehandle) { die "unable to open tmp file: $?"; }
-
-    print $filehandle $ex;
-    seek $filehandle, 0, 0; # rewind file
+if (! $exfile eq "") {
+    open(FILEH,"<$exfile") || die "unable to open file: $?";
+    $filehandle = *FILEH;
     $interactive = 0;
 } else {
-    $filehandle = *STDIN;
-    $interactive = 1;
-}
+    if (! $ex eq "") {
+	$filehandle = tempfile();
 
+	if (!$filehandle) { die "unable to open tmp file: $?"; }
 
+	print $filehandle $ex;
+	seek $filehandle, 0, 0; # rewind file
+	$interactive = 0;
+    } else {
+	$filehandle = *STDIN;
+	$interactive = 1;
+    }
+}
 
 sub list_cb {
 
@@ -122,6 +130,8 @@ sub prompt {
       return $authname;
   } elsif (($type eq "realm") && (defined $realm)) {
       return $realm;
+  } elsif (($type eq "password") && (defined $password)) {
+      return $password;
   }
 
   my $ostty;
@@ -172,6 +182,8 @@ if (!defined $obj) {
 
 my $term = Term::ReadLine->new("sieveshell");
 
+my $exitcode = 0;
+
 $term->ornaments(0);
 
 while(defined($_  = ($interactive ? $term->readline('> ') : <$filehandle>))){
@@ -198,6 +210,9 @@ while(defined($_  = ($interactive ? $ter
 	my $errstr = sieve_get_error($obj);
 	$errstr = "unknown error" if(!defined($errstr));
 	print "upload failed: $errstr\n"; 
+	$exitcode = 1;
+      } else {
+        $exitcode = 0;
       }
     } elsif (($words[0] eq "list") || 
 	     ($words[0] eq "l") || 
@@ -207,6 +222,9 @@ while(defined($_  = ($interactive ? $ter
 	    my $errstr = sieve_get_error($obj);
 	    $errstr = "unknown error" if(!defined($errstr));
 	    print "list failed: $errstr\n";
+	    $exitcode = 1;
+	} else {
+	    $exitcode = 0;
 	}
     } elsif (($words[0] eq "activate") || 
 	     ($words[0] eq "a")) {
@@ -219,6 +237,9 @@ while(defined($_  = ($interactive ? $ter
 	    my $errstr = sieve_get_error($obj);
 	    $errstr = "unknown error" if(!defined($errstr));
 	    print "activate failed: $errstr\n";
+	    $exitcode = 1;
+	} else {
+	    $exitcode = 0;
 	}
     } elsif (($words[0] eq "deactivate") || 
 	     ($words[0] eq "da")) {
@@ -231,6 +252,9 @@ while(defined($_  = ($interactive ? $ter
 	    my $errstr = sieve_get_error($obj);
 	    $errstr = "unknown error" if(!defined($errstr));
 	    print "deactivate failed: $errstr\n";
+	    $exitcode = 1;
+	} else {
+	    $exitcode = 0;
 	}
     } elsif (($words[0] eq "delete") || 
 	     ($words[0] eq "d")) {    
@@ -243,6 +267,9 @@ while(defined($_  = ($interactive ? $ter
 	    my $errstr = sieve_get_error($obj);
 	    $errstr = "unknown error" if(!defined($errstr));
 	    print "delete failed: $errstr\n"; 
+	    $exitcode = 1;
+	} else {
+	    $exitcode = 0;
 	}
     } elsif (($words[0] eq "get") || 
 	     ($words[0] eq "g")) {
@@ -256,25 +283,32 @@ while(defined($_  = ($interactive ? $ter
 	    my $errstr = sieve_get_error($obj);
 	    $errstr = "unknown error" if(!defined($errstr));
 	    print "get failed: $errstr\n"; 
+	    $exitcode = 1;
 	} else {
 	    if ($words[2]) {
 		open (OUTPUT,">$words[2]") || die "Unable to open $words[2]";
 		print OUTPUT $str;
 		close(OUTPUT);
+		$exitcode = 0;
 	    } else {
 		print $str;
+		$exitcode = 0;
 	    }
 	}
     } elsif (($words[0] eq "quit") || ($words[0] eq "q")) {
         sieve_logout($obj);
-	exit 0;
+	exit $exitcode;
     } elsif (($words[0] eq "help") || ($words[0] eq "?")) {
 	show_help();
+	$exitcode = 0;
     } else {
 	print "Invalid command: $words[0]\n";
+	$exitcode = 1;
     } 
 }
 
+exit $exitcode;
+
 __END__
 
 =head1 NAME
@@ -284,7 +318,8 @@ sieveshell - remotely manipulate sieve s
 =head1 SYNOPSIS
 
 sieveshell [B<--user>=I<user>] [B<--authname>=I<authname>] 
-[B<--realm>=I<realm>] [B<--exec>=I<script>] I<server>[B<:>I<port>]
+[B<--realm>=I<realm>] [B<--password>=I<password>]
+[B<--exec>=I<script>] [B<--execfile>=I<file>] I<server>[B<:>I<port>]
 
 sieveshell B<--help>
 
@@ -328,11 +363,21 @@ The user to use for authentication (defa
 
 The realm to attempt authentication in.
 
+=item B<-p> I<password>, B<--password>=I<password>
+
+The password to use when authenticating to server. Note that this
+parameter can be seen in the process list. B<Use with caution!>
+
 =item B<-e> I<script>, B<--exec>=I<script> 
 
 Instead of working interactively, run commands from I<script>, and
 exit when done.
 
+=item B<-f> I<file>, B<--execfile>=I<file>
+
+Instead of working interactively, run commands from file I<file> and
+exit when done.
+
 =back
 
 =head1 REFERENCES
